$(function () {
    $("body").on('click','#createMaterial',function () {//新建素材
        var mouldId = $("#mouldId").val();
        initCreateMaterialWind(mouldId,0,function (material) {//回调
            var context =  getContext(material);
            $("#materialList").append(context);
            $("#materialList").parent().removeClass("hide");
            $(".noDataTipsBox").addClass('hide');
        });
    }).on('click','#selectMaterial',function () {//选择素材
        selectMaterial(1,0,mouldId,'',subjectIds)
    }).on('click','#materialList i.fa-remove',function () {//删除素材
        $(this).parents(".list-group-item").remove();
        var list = $("#materialList>li");
        if(list==null || list.length <= 0){
            $(".noDataTipsBox").removeClass("hide");
            $("#materialList").parent().addClass("hide");
        }
    }).on('click','#save',function () {
        var liList = $("#materialList>li");
        var materialIds = '';
        if(liList!=null && liList.length>0){
            materialIds = liList.map(function () {
                return $(this).data('material-id');
            }).get().join(",");
        }
        $.ajax({
            url:baseloaction +'/admin/course/mould/saveCouMouldMaterial',
            type:'post',
            data:{
                'mouldId':mouldId,
                'materialIds':materialIds
            },
            dataType:'json',
            success:function (result) {
                if(result.success==false){
                    icModale(0, result.message);
                }else{
                    window.location.href = baseloaction +'/admin/course/mould/findMouldList';
                }
            },
            error:function (error) {
                icModale(0, '系统错误，保存模块素材数据保存失败');
            }
        });
    }).on('click','#cancelLast',function () {//取消上一步
        window.location.href=baseloaction+"/admin/course/mould/initUpdate?id="+$("#mouldId").val();
    }).on('click',"#cancel,#returnBtn",function () {//取消
        window.location.href = baseloaction +'/admin/course/mould/findMouldList';
    }).on('click','#fristStep',function(){
        window.location.href=baseloaction+"/admin/course/mould/initUpdate?id="+$("#mouldId").val();
    });
});

/**
 * 选择素材回调
 * @param data 素材列表
 */
function selectMaterialCallback(data) {
    if(data!=null && data.length>0){
        $.each(data,function (index,material) {
            var materialId = material.id;
            var list = $("#materialList>li");
            var status = true;
            if(list!=null && list.length>0){
                list.each(function () {
                   var em = $(this);
                   var _materialId = em.data('material-id');
                   if(_materialId==materialId){
                       status = false;
                   }
                });
            }
            if(status){
                var context = getContext(material);
                $("#materialList").append(context);
                $(".noDataTipsBox").addClass('hide');
                $("#materialList").parent().removeClass("hide");
            }
        });
    }
}

/**
 * 获取内容
 * @param material 素材对象
 */
function getContext(material) {
    var context = '<li class="list-group-item" data-material-id="'+material.id+'">';
    if(finish==1){
        context+='<span class="pull-right"><a href="javascript:void(0)" title="移除" class="c-999 fs12"><i class="fa fa-remove"></i></a></span>';
    }
    context+='<span class="fs12"><i class="fa fa-file"></i>&nbsp;&nbsp;<span>'+material.name+'</span></span></li>';
    return context;
}